package com.outsourcing.bottle.adapter;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.outsourcing.bottle.R;

public class TabAdapter extends BaseAdapter {

	private Context mContext;
	private List<String> mList;
	private int mSelectedTab;

	public TabAdapter(Context context, List<String> list) {
		mContext = context;
		/* attrs.xml <declare-styleable> */
		Theme theme = mContext.getTheme();
		TypedArray a = theme.obtainStyledAttributes(R.styleable.Gallery);
		a.recycle();
		// styleable
		if (list == null)
			list = Collections.emptyList();
		mList = list;
	}

	public void setSelectedTab(int tab) {
		if (tab != mSelectedTab) {
			mSelectedTab = tab;
			notifyDataSetChanged();
		}
	}

	public int getSelectedTab() {
		return mSelectedTab;
	}

	public int getCount() {
		return Integer.MAX_VALUE;
	}

	public Object getItem(int position) {
		return mList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder viewHolder = null;
		 
//		TextView text = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.gallery_item,
					 null);
			viewHolder.tView = (TextView)convertView.findViewById(R.id.tv_title);
			viewHolder.iVMenu = (ImageView)convertView.findViewById(R.id.iv_menu);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.tView.setText(mList.get(position%mList.size()));
		convertView.setLayoutParams(new Gallery.LayoutParams(130, 40));
		
//		text.setTextColor(Color.WHITE);
//		text.setText(mList.get(position % mList.size()));
//		text.setLayoutParams(new Gallery.LayoutParams(130, 40));
//		text.setGravity(Gravity.CENTER);
//		if (position == mSelectedTab) {
//			text.setBackgroundResource(R.drawable.tab_button_select);
//		} else {
//			text.setBackgroundResource(R.drawable.tab_button_unselect);
//		}
		return convertView;
	}

	static class ViewHolder {
		public TextView tView;
		public ImageView iVMenu;
	}

}